import math

from database.mysqlhelper import MySqLHelper
from utils import read_config

db = MySqLHelper()

insert_params = 'insert into  t_stock_month_k (f_code,f_rq,f_kp,f_sp,f_max,f_min) VALUES (%s, %s,%s,%s,%s,%s) '
select_params = 'select f_code,f_rq,f_kp,f_sp,f_max,f_min,f_update_time from t_stock_month_k '


# 添加一条
# data {}
def insert(data):
    # 添加一条
    sql1 = insert_params
    li = (
        data['code'], data['rq'], float(data['kp']), float(data['sp']),
        float(data['max']), float(data['min']))
    count = db.insertone(sql1, li)
    return count


# 添加多条
# data = []
def insert_multiple(data_list):
    d_len = len(data_list)
    if d_len == 0:
        return 0
    # 增加多条
    sql1 = insert_params
    c_len = math.ceil(d_len / 100)
    count = 0
    for i in range(c_len):
        li = []
        start = min(i * 100, d_len)
        end = min(start + 100, d_len)
        if start == end:
            break
        for data in data_list[start:end]:
            li.append((
                data['code'], data['rq'], float(data['kp']), float(data['sp']),
                float(data['max']), float(data['min'])))
        count += db.insertmany(sql1, li)
    return count


# 获取最新的一条
def select(code):
    sql1 = select_params + ' where f_code = %s order by f_rq desc limit 1 '
    return format_one(db.selectone(sql1, code))


# 统计条数
def select_count(code):
    sql1 = 'select count(1) from t_stock_month_k where f_code = %s '
    return db.selectone(sql1, code)[0]


# 获取全部
def select_all(code):
    sql1 = select_params + ' where f_code = %s order by f_rq desc '
    return format_res(db.selectall(sql1, code))


# 获取最新的n条
def select_size(code, size):
    sql1 = select_params + ' where f_code = %s order by f_rq desc limit %s '
    res = db.selectall(sql1, (code, size))
    return format_res(res)


# 获取区间值
def select_interval(code, beg_rq, end_rq):
    # 获取多条
    sql1 = select_params + ' where f_code = %s and f_rq between %s and %s order by f_rq desc '
    return format_res(db.selectall(sql1, (code, beg_rq, end_rq)))


# 更新一条数据
def update(data):
    sql1 = 'update t_stock_month_k set f_kp = %s, f_sp = %s, f_max = %s, f_min = %s ' \
           'where f_code = %s and f_rq = %s '
    return db.update(sql1, (data['kp'], data['sp'], data['max'], data['min'], data['code'], data['rq']))


# 更新数据根据月+更新日期
def update_month(data, year_month):
    sql1 = 'update t_stock_month_k set f_rq = %s,f_kp = %s, f_sp = %s, f_max = %s, f_min = %s ' \
           'where f_code = %s and left(f_rq,7) = %s '
    return db.update(sql1,
                     (data['rq'], data['kp'], data['sp'], data['max'], data['min'], data['code'], year_month))


# 删除数值区间
def delete_interval(code, rq):
    sql1 = 'delete from t_stock_month_k where f_code = %s and f_rq >= %s '
    return db.delete(sql1, (code, rq))


# 格式化结果
def format_one(res):
    if not res:
        return {}
    return {'code': str(res[0], encoding="utf-8"), 'rq': res[1], 'kp': float(res[2]), 'sp': float(res[3]),
            'max': float(res[4]), 'min': float(res[5]), 'up_time': res[6]}


# 格式化结果
def format_res(res):
    if not res:
        return []
    list = []
    for p in res:
        list.append(format_one(p))
    return list


if __name__ == '__main__':
    print('start')
    # count = delete_interval('000000', '2021-06-15')
    count = select_count('000000')
    print(count)
